//
//  DKAuthHomeProductView.m
//  DanKU
//
//  Created by 蓝鳍互娱 on 2023/12/6.
//

#import "DKAuthHomeProductView.h"
#import "DKAuthProductSigleView.h"
@interface DKAuthHomeProductView()
@property (nonatomic, strong) UILabel * dk_AuthHomeProductAlertLab;
@property (nonatomic, strong) DKAuthProductSigleView *dk_AuthProductSigleFirstView;
@property (nonatomic, strong) DKAuthProductSigleView *dk_AuthProductSigleSecondView;
@property (nonatomic, strong) DKAuthProductSigleView *dk_AuthProductSigleThreeView;
@property (nonatomic, strong) UIButton * dk_AuthProductApplyBtn;
@end
@implementation DKAuthHomeProductView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.clearColor;
        [self creatSubView];
    }
    return self;
}
- (void)creatSubView{
    WeakSelf(self)
    self.dk_AuthHomeProductAlertLab = [[UILabel alloc] init];
    self.dk_AuthHomeProductAlertLab.textColor = DK_Hex(@"#999999");
    self.dk_AuthHomeProductAlertLab.font = DK_FONT_Medium(14);
    [self addSubview:self.dk_AuthHomeProductAlertLab];
    [self.dk_AuthHomeProductAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(16)));
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    CGFloat authProductSigleW = (DK_SCREEN_W - DK_IsWidthScale(52))/3;
    self.dk_AuthProductSigleFirstView = [[DKAuthProductSigleView alloc] init];
    self.dk_AuthProductSigleFirstView.productIndex = 1;
    [self addSubview:self.dk_AuthProductSigleFirstView];
    [self.dk_AuthProductSigleFirstView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(16)));
        make.top.equalTo(self.dk_AuthHomeProductAlertLab.mas_bottom).offset(DK_IsWidthScale(10));
        make.height.equalTo(@(DK_IsWidthScale(129)));
        make.width.equalTo(@(authProductSigleW));
    }];
    self.dk_AuthProductSigleFirstView.SelectProductBlock = ^(DKUserProductPackageModel * _Nonnull dk_UserProductPackageMdl) {
        [weakSelf productSigleApplyNow:dk_UserProductPackageMdl];
    };
    
    self.dk_AuthProductSigleSecondView = [[DKAuthProductSigleView alloc] init];
    self.dk_AuthProductSigleSecondView.productIndex = 2;
    [self addSubview:self.dk_AuthProductSigleSecondView];
    [self.dk_AuthProductSigleSecondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_AuthProductSigleFirstView.mas_right).offset(DK_IsWidthScale(10));
        make.top.equalTo(self.dk_AuthProductSigleFirstView);
        make.height.equalTo(self.dk_AuthProductSigleFirstView);
        make.width.equalTo(self.dk_AuthProductSigleFirstView);
    }];
    self.dk_AuthProductSigleSecondView.SelectProductBlock = ^(DKUserProductPackageModel * _Nonnull dk_UserProductPackageMdl) {
        [weakSelf productSigleApplyNow:dk_UserProductPackageMdl];
    };
    
    self.dk_AuthProductSigleThreeView = [[DKAuthProductSigleView alloc] init];
    self.dk_AuthProductSigleThreeView.productIndex = 3;
    [self addSubview:self.dk_AuthProductSigleThreeView];
    [self.dk_AuthProductSigleThreeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_AuthProductSigleSecondView.mas_right).offset(DK_IsWidthScale(10));
        make.top.equalTo(self.dk_AuthProductSigleSecondView);
        make.height.equalTo(self.dk_AuthProductSigleSecondView);
        make.width.equalTo(self.dk_AuthProductSigleSecondView);
    }]; 
    self.dk_AuthProductSigleThreeView.SelectProductBlock = ^(DKUserProductPackageModel * _Nonnull dk_UserProductPackageMdl) {
        [weakSelf productSigleApplyNow:dk_UserProductPackageMdl];
    };
    
    self.dk_AuthProductApplyBtn = [[UIButton alloc] init];
    [self.dk_AuthProductApplyBtn setTitle:@"Apply now" forState:UIControlStateNormal];
    [self.dk_AuthProductApplyBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_AuthProductApplyBtn.titleLabel.font = DK_FONT_Medium(16);
    [self.dk_AuthProductApplyBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_AuthProductApplyBtn addTarget:self action:@selector(productApplyNow) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.dk_AuthProductApplyBtn];
    [self.dk_AuthProductApplyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(self.dk_AuthProductSigleFirstView.mas_bottom).offset(DK_IsWidthScale(20));
    }];
}
- (void)productSigleApplyNow:(DKUserProductPackageModel *)dk_UserProductPackageMdl{
    if(self.SelectSigleProductBlock){
        self.SelectSigleProductBlock(dk_UserProductPackageMdl);
    }
}
- (void)productApplyNow{
    if(self.SelectProductBlock){
        self.SelectProductBlock(self.selectProductList);
    }
}
-(void)setSelectProductList:(NSArray *)selectProductList{
    _selectProductList = selectProductList;
    if(_selectProductList.count >= 3){
        self.dk_AuthHomeProductAlertLab.text = @"Matched three suitable loan products for you";
    }else if (_selectProductList.count == 2){
        self.dk_AuthHomeProductAlertLab.text = @"Matched two suitable loan products for you";
    }else{
        self.dk_AuthHomeProductAlertLab.text = @"Matched one suitable loan product for you";
    }
    [self updataProductList];
//    [self.authHomeProductCollectionView reloadData];
}
- (void)updataProductList{
    if(_selectProductList.count >= 3){
        self.dk_AuthProductSigleFirstView.hidden = NO;
        self.dk_AuthProductSigleSecondView.hidden = NO;
        self.dk_AuthProductSigleThreeView.hidden = NO;
        [self.dk_AuthProductSigleFirstView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(DK_IsWidthScale(16)));
        }];
        [self.dk_AuthProductSigleSecondView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.dk_AuthProductSigleFirstView.mas_right).offset(DK_IsWidthScale(10));
        }];
        [self.dk_AuthProductSigleThreeView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.dk_AuthProductSigleSecondView.mas_right).offset(DK_IsWidthScale(10));
        }];
        self.dk_AuthProductSigleFirstView.dk_UserProductPackMdl = _dk_UserProductPackMdl;
        self.dk_AuthProductSigleSecondView.dk_UserProductPackMdl = _dk_UserProductPackMdl;
        self.dk_AuthProductSigleThreeView.dk_UserProductPackMdl = _dk_UserProductPackMdl;
        self.dk_AuthProductSigleFirstView.dk_UserProductPackageMdl = _selectProductList[0];
        self.dk_AuthProductSigleSecondView.dk_UserProductPackageMdl = _selectProductList[1];
        self.dk_AuthProductSigleThreeView.dk_UserProductPackageMdl = _selectProductList[2];
    }else if (_selectProductList.count == 2){
        self.dk_AuthProductSigleFirstView.hidden = NO;
        self.dk_AuthProductSigleSecondView.hidden = NO;
        self.dk_AuthProductSigleThreeView.hidden = YES;
        CGFloat authProductSigleW = (DK_SCREEN_W - DK_IsWidthScale(52))/3;
        CGFloat authProductSigleX = (DK_SCREEN_W - authProductSigleW*2 - DK_IsWidthScale(10))/2;
        [self.dk_AuthProductSigleFirstView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(authProductSigleX));
        }];
        [self.dk_AuthProductSigleSecondView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.dk_AuthProductSigleFirstView.mas_right).offset(DK_IsWidthScale(10));
        }];
        self.dk_AuthProductSigleFirstView.dk_UserProductPackMdl = _dk_UserProductPackMdl;
        self.dk_AuthProductSigleSecondView.dk_UserProductPackMdl = _dk_UserProductPackMdl;
        self.dk_AuthProductSigleFirstView.dk_UserProductPackageMdl = _selectProductList[0];
        self.dk_AuthProductSigleSecondView.dk_UserProductPackageMdl = _selectProductList[1];
    }else{
        self.dk_AuthProductSigleFirstView.hidden = NO;
        self.dk_AuthProductSigleSecondView.hidden = YES;
        self.dk_AuthProductSigleThreeView.hidden = YES;
        CGFloat authProductSigleW = (DK_SCREEN_W - DK_IsWidthScale(52))/3;
        CGFloat authProductSigleX = (DK_SCREEN_W - authProductSigleW)/2;
        [self.dk_AuthProductSigleFirstView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(authProductSigleX));
        }];
        self.dk_AuthProductSigleFirstView.dk_UserProductPackMdl = _dk_UserProductPackMdl;
        self.dk_AuthProductSigleFirstView.dk_UserProductPackageMdl = _selectProductList[0];
    }
}
//#pragma mark -- UICollectionViewDataSource
//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.selectProductList.count;
//}
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    DKAuthProductCollectionCell *authProductCollectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DKAuthProductCollectionCell" forIndexPath:indexPath];
//    authProductCollectionCell.dk_UserProductPackageMdl = self.selectProductList[indexPath.row];
//    authProductCollectionCell.productIndex = indexPath.row;
//    return authProductCollectionCell;
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat cellWidth = (DK_SCREEN_W - DK_IsWidthScale(60)-1)/3;
//    CGSize cellItemSize = CGSizeMake(cellWidth, cellWidth+DK_IsWidthScale(26));
//    return cellItemSize;
//}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    UIEdgeInsets kPaoSectionEdgeInsets = UIEdgeInsetsZero;
//    kPaoSectionEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
//    return kPaoSectionEdgeInsets;
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    return 0;
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return DK_IsWidthScale(10);
//}
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    DKUserProductPackageModel * dk_UserProductPackageMdl = self.selectProductList[indexPath.row];
//    if(self.SelectProductBlock){
//        self.SelectProductBlock(dk_UserProductPackageMdl);
//    }
//}
@end
